package com.qdxwx.manage.controller.member;

import com.qdxwx.common.json.AjaxResult;
import com.qdxwx.common.page.PageRequest;
import com.qdxwx.common.page.TableDataInfo;
import com.qdxwx.common.utils.TKConvert;
import com.qdxwx.common.utils.TKString;
import com.qdxwx.data.service.course.CourseMemberService;
import com.qdxwx.data.service.course.CourseService;
import com.qdxwx.data.service.subject.SubjectService;
import com.qdxwx.manage.controller.BaseController;
import com.qdxwx.models.course.Course;
import com.qdxwx.models.course.CourseQuery;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import jakarta.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * 会员类型Controller
 * Qdx NetSchool Platform
 * @author qdxwx
 * @updateTime 2022/5/25
 * @description
 */
@Controller
@RequestMapping("/member/course")
public class MemberCourseController extends BaseController {
    @Autowired
    private CourseMemberService courseMemberService;
    @Autowired
    private CourseService courseService;
    @Autowired
    private SubjectService subjectService;

    @GetMapping("/list.html")
    public String list(HttpServletRequest request, ModelMap mmap) {
        long id = TKConvert.toLong(request.getParameter("id"));
        if (id < 1) return error404;
        mmap.put("typeId", id);
        mmap.put("subjectList", subjectService.getList());
        return "member/member-course-list";
    }

    @PostMapping("/list.json")
    @ResponseBody
    public TableDataInfo CourseList(HttpServletRequest request) {
        CourseQuery query = new CourseQuery();
        query.setMemberTypeId(TKConvert.toInteger(request.getParameter("typeId"), null));
        query.setCourseName(TKString.queryKey(request.getParameter("courseName")));
        query.setSubjectId(TKConvert.toInteger(request.getParameter("subjectId"), null));
        List<Course> list = courseService.getList(query, PageRequest.form(request));
        TableDataInfo rspData = new TableDataInfo();
        rspData.setCode(0);
        rspData.setRows(list);
        rspData.setTotal(courseService.count(query));
        return rspData;
    }

    /**
     * 添加会员课程
     */
    @RequestMapping("/add.json")
    @ResponseBody
    public AjaxResult addMemberCourse(HttpServletRequest request) {
        int typeId = TKConvert.toInteger(request.getParameter("typeId"));
        if (typeId < 1) return error("类型错误");
        String[] ids = TKConvert.toStrArray(request.getParameter("courseIds"));
        if (ids.length < 1) return error("至少选择一条课程");
        for (String courseId : ids) {
            /* 去重添加*/
            courseMemberService.insert(typeId, TKConvert.toInteger(courseId));
        }
        return success();
    }

    /**
     * 删除会员课程
     */
    @RequestMapping("/del.json")
    @ResponseBody
    public AjaxResult delMemberCourse(HttpServletRequest request) {
        int courseId = TKConvert.toInteger(request.getParameter("courseId"));
        int typeId = TKConvert.toInteger(request.getParameter("typeId"));
        if (courseId < 1 || typeId < 1) return error("参数异常");
        int row = courseMemberService.delete(typeId, courseId);
        return toAjax(row);
    }

}
